package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.common.dialog.ECProgressDialog;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import java.io.IOException;
import java.io.InputStream;

import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;

/**
 * 修改个人信息的姓名和国籍
 *
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/22
 */

public class EditInfoActivity extends BaseChatActivity implements ICommonViewUi {


    @InjectView(R.id.alias_edt)
    EditText aliasEdt;
    @InjectView(R.id.country_img)
    ImageView countryImg;
    @InjectView(R.id.country_name_tv)
    TextView countryNameTv;
    @InjectView(R.id.country_select_layout)
    RelativeLayout countrySelectLayout;
    @InjectView(R.id.age_edt)
    EditText ageEdt;

    private static final int FLAG_REQUEST_CODE=1;

    private ICommonRequestPresenter iCommonRequestPresenter;

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_edit_profile;
    }

    UserInfo userInfo;
    @Override
    protected void initViewsAndEvents() {
        setTitle("Edit Profile");
        setRightTitle("Confirm");
        iCommonRequestPresenter = new CommonRequestPresenterImpl(mContext, this);
        String data= PreferenceUtils.getPrefString(this, Contants.Preference.user_info,"");
        if (!data.equals("")){
            JsonHelper<UserInfo> jsonHelper = new JsonHelper<UserInfo>(UserInfo.class);
            userInfo = jsonHelper.getData(data, "result");
            bindData();
        }
    }

    private void bindData(){
        aliasEdt.setText(userInfo.getNickName());
        try {
            InputStream is=getAssets().open("flag/"+userInfo.getCountry()+".png");
            countryImg.setImageBitmap(BitmapFactory.decodeStream(is));
            countryNameTv.setText(userInfo.getCountry());
            ageEdt.setText(userInfo.getAge());
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK&&data!=null){
            switch (requestCode){
                case FLAG_REQUEST_CODE:
                    String flagName=data.getStringExtra(FlagSelectActivity.FLAG_NAME);
                    Log.e("flag",flagName+"");
                    try {
                        InputStream is=getAssets().open("flag/"+flagName+".png");
                        countryImg.setImageBitmap(BitmapFactory.decodeStream(is));
                        countryNameTv.setText(flagName);
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

    @OnClick(R.id.toolbar_right_title)
    public void onRightClick(){
        if (TextUtils.isEmpty(aliasEdt.getText().toString().trim())){
            showToastLong("Please enter your name");
            return;
        }

        if (TextUtils.isEmpty(ageEdt.getText().toString().trim())){
            showToastLong("Please enter your age");
            return;
        }
        toRequest(ApiConstants.EventTags.BEGIN_EVENT);
    }

    @OnClick(R.id.country_select_layout)
    public void onSelectClick(){
        Intent intent=new Intent(this,FlagSelectActivity.class);
        startActivityForResult(intent,FLAG_REQUEST_CODE);
    }

    @Override
    public void toRequest(int eventTag) {
        if (userInfo==null){
            return;
        }
        RequestParams params = new RequestParams();
        params.put("accountName", userInfo.getAccountName());
        params.put("nickName", aliasEdt.getText().toString().trim());
        params.put("age", ageEdt.getText().toString().trim());
        params.put("country", countryNameTv.getText().toString().trim());
        iCommonRequestPresenter.request(eventTag, mContext, ApiConstants.Urls.UPDATE_INFO, params);
    }

    @Override
    public void getRequestData(int eventTag, String result) {
//        showToastLong(HttpStatusUtil.getStatusMsg(result));
        PostResult postResult = new PostResult();
        postResult.setTag(EventBusTags.UPDATE_INFO);
        EventBus.getDefault().post(postResult);
        finish();
    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {
        showToastLong(msg);
    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {
        showToastLong(msg);
    }
    private ECProgressDialog mPostingdialog;
    @Override
    public void isRequesting(int eventTag, boolean status) {
        if(status){
            mPostingdialog = new ECProgressDialog(this, "Waiting");
            mPostingdialog.show();
        }else{
            mPostingdialog.dismiss();
        }
    }
}
